#-*-coding:utf-8-*-
from PyQt5.QtWidgets import QApplication, QCalendarWidget, QLabel, QVBoxLayout,QPushButton,QHBoxLayout
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QBasicTimer,pyqtSignal


class Calendar(QtWidgets.QWidget):

    # 自定义信号,需要在类最高级别
    dateSignal = pyqtSignal(str)  # 放个信号来实现跨窗口信息传递


    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self)

        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Calendar')
        self.cal = QCalendarWidget(self)



        self.cal.setGridVisible(True)
        self.cal.selectionChanged.connect(self.showDate)
        self.label = QLabel(self)
        self.getdate = self.cal.selectedDate()
        self.strDate =str(self.getdate.toPyDate())
        self.label.setText(self.strDate)

        self.date=self.strDate

        #确认按钮布局
        self.btn_layout=QHBoxLayout()
        self.btn=QPushButton('确认')
        self.btn.setFixedWidth(130)
        self.btn.clicked.connect(self.click_btn)
        self.btn_layout.addWidget(self.btn)
        self.btn_layout.setAlignment(Qt.AlignRight)

        vbox = QVBoxLayout()
        vbox.addWidget(self.cal)
        vbox.addWidget(self.label)
        vbox.addLayout(self.btn_layout)

        self.setLayout(vbox)

    def showDate(self):
        date = self.cal.selectedDate()
        self.label.setText(str(date.toPyDate()))


    # def mouseDoubleClickEvent(self, QMouseEvent):
    #     print(QMouseEvent)
    #     print(self.date.toPyDate())

    def click_btn(self):
        # self.date=str(self.cal.selectedDate().toPyDate())#self.cal.selectedDate().toPyDate()
        #
        # print(self.date)
        content =str(self.cal.selectedDate().toPyDate())
        self.dateSignal.emit(content)  # 发射信号
        self.close()




    # def doubule_click_func(self):
    #     print(self.date.toPyDate())

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    qb = Calendar()
    qb.show()
    sys.exit(app.exec_())
